package com.tuling.enumdemo;

import java.util.Arrays;
import java.util.Optional;

public enum PinType {

    REGISTER(100000, "注册使用"),
    FORGET_PASSWORD(100001, "忘记密码使用"),
    UPDATE_PHONE_NUMBER(100002, "全新手机号码使用");


    private final int code;

    private final String message;

    PinType(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    @Override
    public String toString() {
        return "PinType{" +
                "code=" + code +
                ", message='" + message + '\'' +
                '}';
    }


    public static PinType get(int code) {
        for (PinType pinType : PinType.values()) {
            if (pinType.code == code) {
                return pinType;
            }
        }
        return null;
    }

    public static PinType getEnum(int value) {
        PinType[] values = values();
        Optional<PinType> first = Arrays.stream(values).filter((pinType) -> pinType.code == value).findFirst();
        return first.orElse(REGISTER);
    }


    public static void main(String[] args) {
        System.out.println(PinType.get(100001).getMessage());
        System.out.println(PinType.getEnum(100002).getMessage());
        System.out.println(PinType.getEnum(100004).getMessage());
    }

}
